<?php
/**
 * Created by PhpStorm.
 * User: mf
 * Date: 2017/9/5
 * Time: 19:37
 */
namespace app\admin\logic;

use think\Db;
use tool\Common;

class Index extends Base
{
    //得到商家主页商品
    public function index()
    {
        $roleID = $this->getRoleID();
        if ($roleID == 1 || $roleID == 2) {
            $sellerID = $this->getUserID();
        } elseif ($roleID == 3 || $roleID == 4 || $roleID == 5) {
            $sellerID = $this->data['sellerID'];
        } else {
            return Common::rm(-2,'您没有权限操作');
        }
        $catalogID = [];
        $catalogs = [];
        $brandID = [];
        $brands = [];
        if (!isset($this->data['catalogID']) && !isset($this->data['brandID'])) {
            $productInfo = Db::view('product','catalogID,brandID')
                ->view('cat',['name' => 'catName'],'cat.id = product.catalogID')
                ->view('brand',['name' => 'brandName'],'cat.id = product.catalogID')
                ->where('sellerID',$sellerID)->select();
            if (!$productInfo) {
                return Common::rm(-3,'商家产品分类和品牌为空');
            }
            foreach ($productInfo as $key => $val) {
                if (!in_array($val['catalogID'],$catalogID)) {
                    $catalogID[] = $val['catalogID'];
                    $catalogs[$key]['catalogID'] = $val['catalogID'];
                    $catalogs[$key]['catName'] = $val['catName'];
                }
                if (!in_array($val['brandID'],$brandID)) {
                    $brandID[] = $val['brandID'];
                    $brands[$key]['brandID'] = $val['brandID'];
                    $brands[$key]['brandName'] = $val['brandName'];
                }
            }
            $product = Db::view('product',['id' => 'productID','name,protocolPrice,catalogID,brandID,marketsPrice'])
                ->view('product_content','titleImage','product_content.productID = product.id')
                ->view('brand',['name' => 'brandName'],'brand.id = product.brandID')
                ->view('user','sellerName','user.id = product.sellerID')
                ->where('sellerID',$this->data['sellerID'])->order('product.addtime desc')->select();
        } else {
            $sql = Db::view('product',['id' => 'productID','name,protocolPrice,catalogID,brandID,marketsPrice'])
                ->view('product_content','titleImage','product_content.productID = product.id')
                ->view('brand',['name' => 'brandName'],'brand.id = product.brandID')
                ->view('user','sellerName','user.id = product.sellerID')
                ->where('sellerID',$this->data['sellerID'])->order('product.addtime desc');
            if (isset($this->data['catalogID']) && !isset($this->data['brandID'])) {
                $product = $sql->where('catalogID',$this->data['catalogID'])->select();
            } elseif (isset($this->data['brandID']) && !isset($this->data['catalogID'])) {
                $product = $sql->where('brandID',$this->data['brandID'])->select();
            } else {
                $product = $sql->where('catalogID',$this->data['catalogID'])->where('brandID',$this->data['brandID'])->select();
            }
        }
        if(!$product) {
            return Common::rm('-2','商家暂时没有上传商品');
        }
        foreach ($product as $_key => $_val) {
            $product[$_key]['name'] = $_val['brandName'].'/'.$_val['name'];
            $product[$_key]['discount'] = round(($_val['marketsPrice'] - $_val['protocolPrice']) / $_val['marketsPrice']*100,2).'%';
            $product[$_key]['titleImage'] = json_decode($_val['titleImage'],true)[0];
            unset($product[$_key]['brandName']);
        }
        return Common::rm(1,'操作成功',[
            'catalogList' => $catalogs,
            'brandList' => $brands,
            'productList' => $product
        ]);
    }
}